/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import db.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class UserDaoImpl implements UserDao{

    @Override
    public User login(String username, String password) {
        Connection conn = null;
	PreparedStatement pstat = null;
	ResultSet rs = null;
	String sql = "select * from user where Name=? ";
        try {
            conn = JDBCUtil.getConnection();
            pstat = conn.prepareStatement(sql);
            pstat.setString(1, username);
            rs = pstat.executeQuery();
            if(rs.next()){
                String pwd=rs.getString("Password");
                System.out.println(pwd);
                if(pwd.equals(password)){
                    JOptionPane.showMessageDialog(null, "<html><font Color='red'>登陆成功！</font>", "信息提示", 3);

                }else{
                   JOptionPane.showMessageDialog(null, "<html><font Color='red'>登陆失败！</font>", "信息提示", 3);
 
                }
            }else{
                JOptionPane.showMessageDialog(null, "<html><font Color='red'>没有此用户！</font>", "信息提示", 3);
 
            }
            
            //rs.
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
	
        return null;
    }
	
	@Override
	public void register(String userName, String password, String telephone, Integer type) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select Name from user where Name=? ";
		String insertSql = "INSERT INTO user (Name,Password,Telephone,Type) VALUES (?,?,?,?)";
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			PreparedStatement pst = conn.prepareStatement(insertSql);
			pstat.setString(1, userName);
			rs = pstat.executeQuery();
			if (rs.next() == false) {
				pst.setString(1, userName);
				pst.setString(2, password);
				pst.setString(3, telephone);
				pst.setInt(4, type);
				int flg = pst.executeUpdate();
				if (flg > 0) {
					JOptionPane.showMessageDialog(null, "<html><font Color='green'>注册成功！</font>", "信息提示", 3);
				} else {
					JOptionPane.showMessageDialog(null, "<html><font Color='red'>注册失败</font>", "信息提示", 3);
				}
			} else {
				JOptionPane.showMessageDialog(null, "<html><font Color='red'>用户名已存在！</font>", "信息提示", 3);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void retrievePassword(String userName, String telephone) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select Name,Password,TelePhone from user where Name=? ";
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, userName);
			rs = pstat.executeQuery();
			if (rs.next()) {
				String phone = rs.getString("TelePhone");
				String pwd = rs.getString("Password");
				if (phone.equals(telephone)) {
					System.out.println("您的密码为：" + pwd);
					JOptionPane.showMessageDialog(null, "<html><font Color='green'>找回密码成功！" + pwd + "</font>", "信息提示",
							3);
				} else {
					JOptionPane.showMessageDialog(null, "<html><font Color='red'>手机号验证不通过！</font>", "信息提示", 3);
				}
			} else {
				JOptionPane.showMessageDialog(null, "<html><font Color='red'>用户不存在！</font>", "信息提示", 3);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
